package com.lz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.lz.enums.DelEnum;
import com.lz.pojo.SysUser;
import com.lz.security.pojo.LoginUser;
import com.lz.security.utils.SecurityUtil;
import com.lz.service.SysUserService;
import com.lz.mapper.SysUserMapper;
import com.lz.utils.MD5Util;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @description 针对表【t_sys_user(用户信息表)】的数据库操作Service实现
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
        implements SysUserService {

    @Override
    public Map<String, Object> pageVo(Integer pageNumber, Integer pageSize, SysUser sysUser) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        //模糊查询 like(执行条件, column–字段, val–值)
        queryWrapper.like(StringUtils.hasLength(sysUser.getUsername()),
                SysUser::getUsername, sysUser.getUsername());
        queryWrapper.eq(SysUser::getDel,DelEnum.ENTER_DEL_NO.getStatus());
        //翻页查询 page(page–翻页对象,queryWrapper实体对象封装操作类)
        IPage<SysUser> page = this.page(new Page<SysUser>(pageNumber, pageSize), queryWrapper);
        Map<String, Object> map = new HashMap<>();
        //获取分页列表数据
        map.put("list", page.getRecords());
        //获取总记录数
        map.put("total", page.getTotal());
        return map;
    }

    @Override
    public SysUser get(String id) {
        return this.baseMapper.selectVoById(id);
    }

    /**
     * 新增用户
     *
     * @param sysUser 用户信息
     * @return 是否新增成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)   //开启事务管理 rollbackFor遇到异常回滚
    public boolean add(SysUser sysUser) {
        //获取登陆用户信息
       /* LoginUser loginuser = SecurityUtil.getLoginuser();
        sysUser.setCreateUser(loginuser.getUsername());
        sysUser.setUpdateUser(loginuser.getUsername());*/
        sysUser.setCreateTime(new Date());
        sysUser.setUpdateTime(new Date());
        if(StringUtils.hasLength(sysUser.getUserpwd())){
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String encodepas = passwordEncoder.encode(sysUser.getUserpwd());
            sysUser.setUserpwd(encodepas);
//            sysUser.setUserpwd(MD5Util.getMD5(sysUser.getUserpwd()));
        }
        //保存用户
        this.save(sysUser);
        //springboot内置工具类CollectionUtils  isEmpty判断 List/Set 是否为空
        if (!CollectionUtils.isEmpty(sysUser.getRoleIdList())) {
            this.baseMapper.insertUserRole(sysUser);
        }
        return true;
    }

    /**
     * 修改用户
     *
     * @param sysUser 用户信息
     * @return 是否修改成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean edit(SysUser sysUser) {
        //获取登陆用户信息
       /* LoginUser loginuser = SecurityUtil.getLoginuser();
        sysUser.setUpdateUser(loginuser.getUsername());*/
        sysUser.setUpdateTime(new Date());
        if(StringUtils.hasLength(sysUser.getUserpwd())){
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String encodepas = passwordEncoder.encode(sysUser.getUserpwd());
            sysUser.setUserpwd(encodepas);
//            sysUser.setUserpwd(MD5Util.getMD5(sysUser.getUserpwd()));
        }
        //修改用户
        this.updateById(sysUser);
        if (sysUser.getRoleIdList() != null) {
            //删除用户关联的角色
            this.baseMapper.deleteUserRole(sysUser.getId());
            //判断角色是否为空
            if (!sysUser.getRoleIdList().isEmpty()) {
                //添加用户关联角色
                this.baseMapper.insertUserRole(sysUser);
            }
        }
        return true;
    }

    @Override
    public boolean editUser(String id) {
        SysUser sysUser = new SysUser();
        sysUser.setId(id);
        sysUser.setDel(DelEnum.ENTER_DEL_YES.getStatus());
        return SqlHelper.retBool(baseMapper.updateById(sysUser));
    }
}




